<?php
class model_sys extends base_model{
	public function __construct() {
		parent::__construct();
	}
    /*
     * 控制器分类
     */
	public function mod_auth_group(){
	    $sql = 'select * from `@@auth_group` order by `sort` asc';
	    return $this->db->query($sql,3);
    }
    public function mod_auth_group_sub($id){
        $sql = 'select * from `@@auth` where `type`='.$id.' order by `sort` asc';
        return $this->db->query($sql,3);
    }
    public function mod_auth_list(){
	    $auth_group = $this->mod_auth_group();
	    foreach ($auth_group as $k=>$v){
	        $auth_group[$k]['sub'] = $this->mod_auth_group_sub($v['id']);
        }
        return $auth_group;
    }
    public function mod_sysuser_user_group_forid($id){
        $sql = 'select * from `@@auth_user_group` where `id`='.$id;
        return $this->db->query($sql,2);
    }

    public function mod_sysuser_user_group(){
        $sql = 'select * from `@@auth_user_group`';
        return $this->db->query($sql,3);
    }
    /*
	 * 取出单行控制器数据记录
	 * @param int $id 品牌ID
	 */
    public function mod_auth_item($id){
        $sql = 'SELECT * FROM `@@auth` WHERE `id`='.$id.' LIMIT 1';
        return $this->db->query($sql,2);
    }

    /*
	* 保存添加或编辑的控制器
	*/
    public function mod_auth_save($para,$update_args=null,$edit=false){
        $result = 0;
        //重组字段
        if($edit){
            $result = $this->db->update('@@auth',$para,$update_args);
        }else{
            $result = $this->db->insert('@@auth',$para);
        }
        return $result;
    }
    /*
     * 保存用户组的权限
     */
    public function mod_user_group_setting_save($ids,$groupid){
        $sql = "update `@@auth_user_group` SET `auths`='{$ids}' WHERE `id`={$groupid}";
        //core::error($sql);
        return $this->db->query($sql,0);
    }
    /*
     * 取出单个管理员信息
     */
    public function mod_sysuser_item($uid){
        $sql = 'select * from `@@admin_members` where `uid`='.$uid.' limit 1';
        return $this->db->query($sql,2);
    }
    /*
     * 系统管理员列表
     */
    public function mod_sysuserlist($args){
        $where_txt = "";
        if(!empty($args['usergroup'])){
            $where_txt .= " AND `usergroup`=".$args['usergroup'];
        }
        if(!empty($args['sKey'])){
            $where_txt .= " AND `username`='{$args['sKey']}'";
        }

        $sql = "SELECT ~field~ FROM `@@admin_members` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
	
	public function mod_admin_loger($args){
		$where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `username`='{$args['sKey']}'";
        }
        $sql = "SELECT ~field~ FROM `@@admin_session` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => ' ORDER BY accesstime DESC',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
	}
	/*
	* 提取自定义导航内容
	*/
	public function mod_mainnav(){
		$sql = 'select * from `@@mainnav`';
		return $this->db->query($sql,3);
	}
	/*
	* 取出单个导航数据
	*/
	public function mod_mainnav_item($id){
		$sql = 'select * from `@@mainnav` where `id`='.$id;
		return $this->db->query($sql,2);
	}

	//积分种类列表
    public function mod_score_category_list($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%{$args['sKey']}%'";
        }

        $sql = "SELECT ~field~ FROM `@@score_category` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    //积分种类状态更新
    public function mod_score_category_state($id){
        $sql = "UPDATE @@score_category SET `state`=ABS(`state`-1) WHERE `id`={$id}";
        $result = $this->db->query($sql,0);
        return $result;
    }
    //取出指定行的积分种类
    public function mod_score_category_item($id){
        $sql = "SELECT * FROM `@@score_category` WHERE `id`={$id} LIMIT 1";
        return $this->db->query($sql,2);
    }
    //取出可用的所有银行
    public function mod_banklist_all()
    {
        $sql = "SELECT `id`,`code`,`title` FROM `@@bank_list` WHERE `state`=1 ORDER BY `sort` ASC";
        return $this->db->query($sql,3);
    }
    //取出单行银行数据
    public function mod_banklist_item($id){
        $sql = "SELECT * FROM `@@bank_list` WHERE `id`={$id} LIMIT 1";
        return $this->db->query($sql,2);
    }
    //删除多行数据
    public function mod_banklist_delete_list($ids){
        $sql = 'DELETE FROM `@@bank_list` WHERE `id` in ('.$ids.')';
        return $this->db->query($sql,0);
    }
    //银行列表
    public function mod_banklist_list($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%{$args['sKey']}%'";
        }

        $sql = "SELECT ~field~ FROM `@@bank_list` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    //删除多行充值通道
    public function mod_payway_delete_list($ids){
        $sql = 'DELETE FROM `@@pay_way` WHERE `id` in ('.$ids.')';
        return $this->db->query($sql,0);
    }
    //拉取单行充值通道数据
    public function mod_payway_item($id){
        $sql = "SELECT * FROM `@@pay_way` WHERE `id`={$id} LIMIT 1";
        return $this->db->query($sql,2);
    }
    //充值通道列表
    public function mod_payway_list($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%{$args['sKey']}%'";
        }

        $sql = "SELECT ~field~ FROM `@@pay_way` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => ' ORDER BY `sort` DESC',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    //充值通道列表（SELECT选项）
    public function mod_payway_select_item()
    {
        $sql = "SELECT `id`,`title`,`payclass`,`info` FROM `@@pay_way` ORDER BY `sort` ASC";
        return $this->db->query($sql,3);
    }
    //充值线路（单行）
    public function mod_rechargeline_item($id)
    {
        $sql = "SELECT l.*,w.`payclass` FROM `@@rechargeline` l,`@@pay_way` w WHERE l.`id`=".$id." AND l.`paywayid`=w.`id` LIMIT 1";
        return $this->db->query($sql,2);
    }
    //充值线路列表
    public function mod_rechargeline_list($args)
    {
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND rl.`title` LIKE '%{$args['sKey']}%'";
        }

        $sql = "SELECT ~field~ FROM `@@rechargeline` rl,`@@pay_way` pw WHERE pw.`id`=rl.`paywayid` {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => 'rl.*,pw.`title` AS paywaytitle',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => ' ORDER BY rl.`sort` DESC',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }

    //
    public function mod_adminactionlog_list($args){
        $where_txt = "";
        if(!empty($args['username'])){
            $where_txt .= " AND `username`='{$args['username']}'";
        }
        if( !empty($args['action']) ){
            $where_txt .= " AND `action`='{$args['action']}'";
        }
        $sql = "SELECT ~field~ FROM `@@admin_log` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => 'order by actiontime desc',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
}